package com.logic.landseaserver.models.news.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.domain.News;
import com.logic.landseaserver.models.news.bean.AllNewsResp;
import com.logic.landseaserver.models.news.bean.DateResp;
import com.logic.landseaserver.models.news.bean.NewsResp;
import com.logic.landseaserver.models.news.service.NewsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Path("/news")
@Api(value = "新闻大事记招采")
public  class NewsRest
{
	private static final Logger LOGGER = LoggerFactory.getLogger(NewsRest.class);

	@Autowired
	private NewsService newsService;

	@POST
	@Path("/saveNews")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "保存新闻招采大事记")
	public Response saveNews(@ApiParam(value = "param", required = false) News req)
	{
		CommonResultMap map = new CommonResultMap();
		try
		{         
			newsService.saveNews(req);
			map.setResult(CommonResult.SUCCESS);
		}
		catch (LandseaException e)
		{
			LoggerHelper.handleException(LOGGER, "保存公告消息失败", e, map);
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}

	@POST
	@Path("/selectNewsList")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "新闻招采大事记分页查询")
	public Response selectNewsList(@ApiParam(value = "param", required = false) QueryUtil queryUtil)
	{
		CommonResultMap map = new CommonResultMap();
		try
		{

			queryUtil.getPagingTool().calculateStartIndex();
			List<NewsResp> list = newsService.selectNewsList(queryUtil);
			Integer totalCount = queryUtil.getPagingTool().getTotalNum();
			map.setResultTotalCount(totalCount);
			map.setData(list);
			map.setResult(CommonResult.SUCCESS);
		}
		catch (LandseaException e)
		{
			LoggerHelper.handleException(LOGGER, "新闻招采大事记分页查询失败", e, map);
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}

	@GET
	@Path("/getNewsDetail/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "查询新闻详情")
	public Response getNewsDetail(
			@ApiParam(value = "param", required = true) @PathParam("id") Integer id)
	{
		CommonResultMap map = new CommonResultMap();
		try
		{
			NewsResp resp  = newsService.getNewsDetail(id);
			map.setData(resp);
			map.setResult(CommonResult.SUCCESS);
		}
		catch (LandseaException e)
		{
			LoggerHelper.handleException(LOGGER, "查询新闻详情失败", e, map);
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
	@POST
	@Path("/editStatus")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "发布/取消发布新闻")
	public Response editStatus(
			@ApiParam(value = "param", required = true) News req)
	{
		CommonResultMap map = new CommonResultMap();
		try
		{
			newsService.editStatus(req);
			map.setResult(CommonResult.SUCCESS);
		}
		catch (LandseaException e)
		{
			LoggerHelper.handleException(LOGGER, "发布/取消发布新闻失败", e, map);
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
	@GET
	@Path("/deleteNews/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "删除新闻")
	public Response deleteNews(
			@ApiParam(value = "param", required = true) @PathParam("id") Integer id)
	{
		CommonResultMap map = new CommonResultMap();
		try
		{
			newsService.deleteNews(id);
			map.setResult(CommonResult.SUCCESS);
		}
		catch (LandseaException e)
		{
			LoggerHelper.handleException(LOGGER, "删除新闻失败", e, map);
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
	@GET
	@Path("/selectMemorabilia")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "关于朗诗寓大事记查询")
	public Response selectMemorabilia()
	{
		CommonResultMap map = new CommonResultMap();
		try
		{
			List<NewsResp> resps = newsService.selectMemorabilia();
			map.setData(resps);
			map.setResult(CommonResult.SUCCESS);
		}
		catch (LandseaException e)
		{
			LoggerHelper.handleException(LOGGER, "关于朗诗寓大事记查询失败", e, map);
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
	@GET
	@Path("/selectNew")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "关于朗诗寓新闻查询")
	public Response selectNew()
	{
		CommonResultMap map = new CommonResultMap();
		try
		{
			List<NewsResp> resps = newsService.selectNew();
			map.setData(resps);
			map.setResult(CommonResult.SUCCESS);
		}
		catch (LandseaException e)
		{
			LoggerHelper.handleException(LOGGER, "关于朗诗寓新闻查询失败", e, map);
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
	@GET
	@Path("/selectDate/{type}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "查询年份日期")
	public Response selectDate(@ApiParam(value = "param", required = false) @PathParam("type") String type)
	{
		CommonResultMap map = new CommonResultMap();
		try
		{         
			DateResp resultResp = newsService.selectDate(type);
			map.setData(resultResp);
			map.setResult(CommonResult.SUCCESS);
		}
		catch (LandseaException e)
		{
			LoggerHelper.handleException(LOGGER, "查询年份日期失败", e, map);
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}

	@POST
	@Path("/selectAllNews")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "查询前端展示新闻大事记招采")
	public Response selectAllNews(@ApiParam(value = "param", required = false) QueryUtil queryUtil)
	{
		CommonResultMap map = new CommonResultMap();
		try
		{         
			queryUtil.getPagingTool().calculateStartIndex();
			AllNewsResp list = newsService.selectAllNews(queryUtil);
			Integer totalCount = queryUtil.getPagingTool().getTotalNum();
			map.setResultTotalCount(totalCount);
			map.setData(list);
			map.setResult(CommonResult.SUCCESS);
		}
		catch (LandseaException e)
		{
			LoggerHelper.handleException(LOGGER, "查询前端展示新闻大事记招采失败", e, map);
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
}
